using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The TeacherAddressTableDB class is responsible for interacting with the database to retrieve and store information
    /// about TeacherAddressTable objects.
    /// </summary>
    public class TeacherAddressTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of TeacherAddressTable from the underlying datasource.
        /// </summary>
        /// <param name="teacherAddressKey">The unique TeacherAddressKey of the TeacherAddressTable in the database.</param>
        /// <returns>An TeacherAddressTable when the TeacherAddressKey was found in the database, or null otherwise.</returns>
        public static TeacherAddressTable GetItem(int teacherAddressKey)
        {
            TeacherAddressTable myTeacherAddressTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTeacherAddressTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@teacherAddressKey", teacherAddressKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myTeacherAddressTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myTeacherAddressTable;
            }
        }

        /// <summary>
        /// Returns a list with TeacherAddressTable objects.
        /// </summary>
        /// <returns>A generics List with the TeacherAddressTable objects.</returns>
        /// 
        public static TeacherAddressTable GetItemByUserAccountKey(int UserAccountKey, Int16 AddressTypeCode)
        {
            TeacherAddressTable myTeacherAddressTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTeacherAddressTableSelectSingleItemByUserAccountKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);
                    myCommand.Parameters.AddWithValue("@AddressTypeCode", AddressTypeCode);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myTeacherAddressTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myTeacherAddressTable;
            }
        }

        public static TeacherAddressTableList GetList()
        {
            TeacherAddressTableList tempList = new TeacherAddressTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTeacherAddressTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Returns a list with TeacherAddressTable objects.
        /// </summary>
        /// <returns>A generics List with the TeacherAddressTable objects.</returns>
        public static TeacherAddressTableList GetListByUserAccountKey(int UserAccountKey)
        {
            TeacherAddressTableList tempList = new TeacherAddressTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTeacherAddressTableSelectListByUserAccountKey", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
                return tempList;
            }
        }

        /// <summary>
        /// Saves a TeacherAddressTable in the database.
        /// </summary>
        /// <param name="myTeacherAddressTable">The TeacherAddressTable instance to save.</param>
        /// <returns>The new TeacherAddressKey if the TeacherAddressTable is new in the database or the existing TeacherAddressKey when an item was updated.</returns>
        public static int Save(TeacherAddressTable myTeacherAddressTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTeacherAddressTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myTeacherAddressTable.TeacherAddressKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@teacherAddressKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@teacherAddressKey", myTeacherAddressTable.TeacherAddressKey);
                    }
                    if (myTeacherAddressTable.UserAccountKey == null)
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@userAccountKey", myTeacherAddressTable.UserAccountKey);
                    }
                    if (string.IsNullOrEmpty(myTeacherAddressTable.UnitNo))
                    {
                        myCommand.Parameters.AddWithValue("@unitNo", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@unitNo", myTeacherAddressTable.UnitNo);
                    }
                    if (string.IsNullOrEmpty(myTeacherAddressTable.LevelNo))
                    {
                        myCommand.Parameters.AddWithValue("@levelNo", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@levelNo", myTeacherAddressTable.LevelNo);
                    }
                    if (string.IsNullOrEmpty(myTeacherAddressTable.StreetName))
                    {
                        myCommand.Parameters.AddWithValue("@streetName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@streetName", myTeacherAddressTable.StreetName);
                    }
                    if (myTeacherAddressTable.TownshipCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@townshipCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@townshipCode", myTeacherAddressTable.TownshipCode);
                    }
                    if (myTeacherAddressTable.CityCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@cityCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@cityCode", myTeacherAddressTable.CityCode);
                    }
                    if (myTeacherAddressTable.DistrictCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@districtCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@districtCode", myTeacherAddressTable.DistrictCode);
                    }
                    if (myTeacherAddressTable.CountryCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@countryCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@countryCode", myTeacherAddressTable.CountryCode);
                    }
                    if (string.IsNullOrEmpty(myTeacherAddressTable.PostalCode))
                    {
                        myCommand.Parameters.AddWithValue("@postalCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@postalCode", myTeacherAddressTable.PostalCode);
                    }
                    if (myTeacherAddressTable.AddressTypeCode == null)
                    {
                        myCommand.Parameters.AddWithValue("@addressTypeCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@addressTypeCode", myTeacherAddressTable.AddressTypeCode);
                    }
                    if (myTeacherAddressTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", myTeacherAddressTable.Status);
                    }
                    if (myTeacherAddressTable.CreatedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", myTeacherAddressTable.CreatedDateTime);
                    }
                    if (myTeacherAddressTable.ModifiedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", myTeacherAddressTable.ModifiedDateTime);
                    }

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a TeacherAddressTable from the database.
        /// </summary>
        /// <param name="teacherAddressKey">The TeacherAddressKey of the TeacherAddressTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int teacherAddressKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocTeacherAddressTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@teacherAddressKey", teacherAddressKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the TeacherAddressTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static TeacherAddressTable FillDataRecord(IDataRecord myDataRecord)
        {
            TeacherAddressTable myTeacherAddressTable = new TeacherAddressTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TeacherAddressKey")))
            {
                myTeacherAddressTable.TeacherAddressKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("TeacherAddressKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
            {
                myTeacherAddressTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UnitNo")))
            {
                myTeacherAddressTable.UnitNo = myDataRecord.GetString(myDataRecord.GetOrdinal("UnitNo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LevelNo")))
            {
                myTeacherAddressTable.LevelNo = myDataRecord.GetString(myDataRecord.GetOrdinal("LevelNo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StreetName")))
            {
                myTeacherAddressTable.StreetName = myDataRecord.GetString(myDataRecord.GetOrdinal("StreetName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TownshipCode")))
            {
                myTeacherAddressTable.TownshipCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("TownshipCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CityCode")))
            {
                myTeacherAddressTable.CityCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CityCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DistrictCode")))
            {
                myTeacherAddressTable.DistrictCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DistrictCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CountryCode")))
            {
                myTeacherAddressTable.CountryCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CountryCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PostalCode")))
            {
                myTeacherAddressTable.PostalCode = myDataRecord.GetString(myDataRecord.GetOrdinal("PostalCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AddressTypeCode")))
            {
                myTeacherAddressTable.AddressTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("AddressTypeCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myTeacherAddressTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myTeacherAddressTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myTeacherAddressTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myTeacherAddressTable;
        }
    }

}
